Ext.namespace('pages.menu.menuUser');

pages.menu.menuUser.Main = function(config) {
	Ext.apply(this, config);
	this.initUIComponents();
	pages.menu.menuUser.Main.superclass.constructor.call(this);
};

Ext.extend(pages.menu.menuUser.Main, Ext.Panel, {
	/**
	 * 构造函数
	 */
	initUIComponents : function() {

		/* grid */
		var sm = new Ext.grid.CheckboxSelectionModel({
					singleSelect : true
				});
		var cm = new Ext.grid.ColumnModel([sm, new Ext.grid.RowNumberer(), {
					header : 'ID',
					width : 100,
					sortable : true,
					hidden : true,
					dataIndex : 'ID'
				}, {
					header : '用户名',
					width : 100,
					sortable : true,
					dataIndex : 'GYH'
				}, {
					header : '姓名',
					width : 100,
					sortable : true,
					dataIndex : 'XM'
				}, {
					header : '岗位名称',
					width : 300,
					sortable : true,
					dataIndex : 'GWMC'
				}]);
		this.store = new Ext.data.Store({
					proxy : new Ext.data.HttpProxy({
								url : ExtJ
										.getActionPath('/cud/queryUserByRoleId.do')
							}),
					remoteSort : true,
					reader : new Ext.data.JsonReader({
								root : 'data',
								totalProperty : 'total',
								id : 'ID'
							}, [{
										name : 'ID',
										mapping : 'ID'
									}, {
										name : 'GYH',
										mapping : 'GYH'
									}, {
										name : 'XM',
										mapping : 'XM'
									}, {
										name : 'GWMC',
										mapping : 'GWMC'
									}])
				});
				
		this.westPanel = this.tree = new Ext.tree.TreePanel({
					region : "west",
					title : '角色',
					width : 150,
					border : false,
					split : true,
					collapsible : true,
					loader : new Ext.tree.TreeLoader({
								baseParams : {
									nodeId : '-1'
								},
								dataUrl : ExtJ
										.getActionPath('role/queryRoleList.do')
							}),
					root : new Ext.tree.AsyncTreeNode({
								id : '-1',
								draggable : false,
								expanded : false,
								text : ''
							}),
					autoScroll : true,
					animate : false,
					border : false,
					enableDD : false,
					containerScroll : true,
					singleExpand : false,
					draggable : false,
					rootVisible : false,
					listeners : {
						'render' : function() {
						}.createDelegate(this),
						'beforeload' : function(node, e) {
							this.tree.getLoader().baseParams.nodeId = node.id;
						}.createDelegate(this),
						'click' : this.onTreeNodeClick.createDelegate(this)
					}
				});
		
		this.grid = new Ext.grid.GridPanel({
					id : 'grid',
					columnWidth : 1,
					height : 474,
					expand : true,
					store : this.store,
					loadMask : true,
					border : true,
					frame : false,
					viewConfig : {
						autoFill : false,
						forceFit : false,
						focusRow : function() {
							return;
						},
						getRowClass : function(record, rowIndex, rowParams,
								store) {
							if (record.get("TOP_LEVEL") == "1") {
								return 'ux-grid-record-onTop';
							}
						}
					},
					sm : sm,
					cm : cm,
					bbar : new Ext.PagingToolbar({
								pageSize : pageSize,
								store : this.store,
								displayInfo : true,
								displayMsg : '当前显示第 {0} - {1} 条 / 共 {2} 条',
								emptyMsg : "没有记录"
							}),
					listeners : {
						'click' : this.onCheckBoxClick.createDelegate(this)
					},
					tbar : [{
								text : '查询',
								iconCls : 'query',
								handler : this.onQuery.createDelegate(this)
							},'-',{
								text : '重置',
								iconCls : 'reset',
								handler : this.onReset.createDelegate(this)
							}]
				});
		this.eastPanel = this.tree = new Ext.tree.TreePanel({
					region : "east",
					title : '菜单',
					width : 300,
					border : false,
					split : true,
					collapsible : true,
					loader : new Ext.tree.TreeLoader({
								baseParams : {
									nodeId : '-1',
									userId : ''
								},
								dataUrl : ExtJ
										.getActionPath('/cud/queryMenuList.do')
							}),
					root : new Ext.tree.AsyncTreeNode({
								id : '-1',
								draggable : false,
								expanded : false,
								text : ''
							}),
					tbar : [{
								id : 'save',
								text : '保存',
								iconCls : 'save',
								handler : this.onSaveRoleMenu
										.createDelegate(this)
							}],
					autoScroll : true,
					animate : false,
					border : false,
					enableDD : false,
					containerScroll : true,
					singleExpand : false,
					draggable : false,
					rootVisible : false,
					listeners : {
						'render' : function() {
						}.createDelegate(this),
						'beforeload' : function(node, e) {
							this.tree.getLoader().baseParams.nodeId = node.id;
						}.createDelegate(this)
					}
				});
		this.eastPanel.on('checkchange', function(node, checked) {
					this.selParent(node, checked);
					this.selChild(node, checked);
				}.createDelegate(this), this.eastPanel);
		
		this.centerPanel = new Ext.form.FormPanel({
					layout : 'column',
					title : '用户',
					region : 'center',
					labelAlign : 'right',
					autoHeight : true,
					border : false,
					frame : false,
					items : [{
								columnWidth : 1,
								layout : 'column',
								region : 'north',
								labelWidth : 75,
								labelAlign : 'right',
								autoHeight : true,
								border : false,
								frame : false,
								items : [{
											columnWidth : 1,
											height : 10,
											border : false
										}, {
											columnWidth : .33,
											layout : 'form',
											border : false,
											items : [{
														fieldLabel : '用户名',
														name : 'p_GYH',
														id : 'p_GYH',
														anchor : '98%',
														xtype : 'textfield'
													}]
										}, {
											columnWidth : .33,
											layout : 'form',
											border : false,
											items : [{
														fieldLabel : '姓名',
														name : 'p_XM',
														id : 'p_XM',
														anchor : '98%',
														xtype : 'textfield'
													}]
										}, {
											columnWidth : .33,
											layout : 'form',
											border : false,
											items : [{
														fieldLabel : '岗位名称',
														name : 'p_GWMC',
														id : 'p_GWMC',
														anchor : '98%',
														xtype : 'textfield'
													}]
										}]
							}, this.grid]
				});
		/**
		 * apply
		 */
		Ext.apply(this, {
					title : '菜单授权(根据人)',
					layout : 'border',
					border : false,
					tools : [{
						id : 'help',
						handler : function(){
							this.onHelp();				
						}.createDelegate(this)
					}],
					items : [this.westPanel, this.centerPanel, this.eastPanel],
					listeners : {
						'render' : this.initData.createDelegate(this)
					}
				})
	},
	
	onHelp : function(){
		var dialog = new global.help.HelpWindow({key : 'xtgl.all.helpmenuUser'});
		dialog.show();
	},
	
	
	initData : function() {
	},
	onQuery : function() {
		var selectNode = this.westPanel.getSelectionModel().getSelectedNode();
		if (!selectNode) {
			Ext.Msg.alert('提示', '请先选择一个角色！');
			return;
		} else {
			this.store.baseParams.roleId = selectNode.id;
			this.store.baseParams.xm = Ext.getCmp('p_XM').getValue();
			this.store.baseParams.gwmc = Ext.getCmp('p_GWMC').getValue();
			this.store.baseParams.gyh = Ext.getCmp('p_GYH').getValue();
			Ext.getBody().mask("正在查询,请稍候...");
			this.store.reload({
						params : {
							start : 0,
							limit : pageSize
						},
						callback : function() {
							Ext.getBody().unmask(true);
						}
					});

		}
	},
	onTreeNodeClick : function(node, e) {
		Ext.getBody().mask("数据加载中，请稍候...");
		var roleId = this.store.baseParams.roleId = node.attributes.id;
		var xm = this.store.baseParams.xm = Ext.getCmp('p_XM').getValue();
		var gwmc = this.store.baseParams.gwmc = Ext.getCmp('p_GWMC').getValue();
		var gyh = this.store.baseParams.gyh = Ext.getCmp('p_GYH').getValue();
		this.store.reload({
					params : {
						start : 0,
						limit : pageSize
					},
					callback : function() {
						Ext.getBody().unmask(true);
					}
				});
	},
	onReset : function() {
		this.centerPanel.getForm().reset();
	},
	onCheckBoxClick : function() {
		var seletedItem = this.grid.getSelectionModel().getSelected();
		if(undefined != seletedItem && null != seletedItem){
			var userId = this.grid.getSelectionModel().getSelected().data.ID;
			this.eastPanel.getLoader().baseParams.userId = userId;
			this.eastPanel.getRootNode().reload();
			this.eastPanel.expandAll();
		}
	},
	onSaveRoleMenu : function() {
		var selected = this.grid.getSelectionModel().getSelected();
		if (!selected) {
			Ext.Msg.alert("警告", "请选择一个用户!");
		} else {
			var userId = selected.data.ID;
			var selectMenu = this.eastPanel.getChecked();
			var menuids = [];
			for (var i = 0; i < selectMenu.length; i++) {
				menuids.push(selectMenu[i].id);
			}
			//console.info(menuids);
			Ext.getBody().mask("数据加载中，请稍候...");
			Ext.Ajax.request({
						url : ExtJ.getActionPath("/cud/saveUserMenu.do"),
						params : {
							menuids : menuids,
							userId : userId
						},
						callback : function(options, suc, response) {
							if (suc) {
								Ext.Msg.alert('提示', '保存成功！');
							} else {
								Ext.Msg.alert('提示', '保存失败！');
							}
							this.eastPanel.getRootNode().reload();
							parent.Ext.getCmp("mainMenu").getRootNode()
									.reload();
							this.eastPanel.expandAll();
							Ext.getBody().unmask(true);
						}.createDelegate(this)
					});
		}
	},
	
	/*
	 * 选中父节点，自动选择所有子节点
	 */
	selParent : function(node, checked) {
		if (node.hasChildNodes()) {
			node.eachChild(function(child) {
						child.attributes.checked = checked;
						var cb = child.ui.checkbox;
						if (cb) {
							cb.checked = checked;
							selParent(child, checked);
						}
					});
		}
	},
	// 这个方法是选择子节点,自动选中父节点的父节点
	selChild : function(node, checked) {
		if (checked) {
			var parentNode = node.parentNode;
			if (parentNode != undefined) {
				parentNode.attributes.checked = checked;
				var cb = parentNode.ui.checkbox;
				if (cb) {
					cb.checked = checked;
					selChild(parentNode, checked);
				}
			}
		} else {
			var flag = false;
			if (node == null) {
				return;
			}
			var parentNode = node.parentNode;
			if (parentNode != undefined) {
				if (parentNode.hasChildNodes()) {
					parentNode.eachChild(function(child) {
								var cd = child.attributes.checked;
								if (cd == true) {
									flag = true;
								}
							});
				}
			}
			if (!flag) {
				parentNode.attributes.checked = checked;
				var cb = parentNode.ui.checkbox;
				if (cb) {
					cb.checked = checked;
					selChild(parentNode, checked);
				}
			}
		}
	}
});